package com.demo.demo.images;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ResourceUtils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
/**
 * 二维码合并logo
 *
 * @Author WuHuang
 * @Date 2023/5/11
 */
public class MergeImages {
    public static void main(String[] args) throws Exception {
        try {
            // 加载二维码和logo图片
            URL url = new URL("https://wework.qpic.cn/wwpic/671307_yUwejkGfTb66Jf8_1683773637/0");
            BufferedImage qrCodeImage = ImageIO.read(url);
            ResourceLoader resourceLoader = new DefaultResourceLoader();
            Resource logoResource = resourceLoader.getResource("classpath:qiwei/logo.png");
            String imagePath = "classpath:lulu/logo.png";
            Path path = Paths.get(ResourceUtils.getURL(imagePath).toURI());
            byte[] imageData = Files.readAllBytes(path);
            BufferedImage logoImage = ImageIO.read(logoResource.getInputStream());

//            BufferedImage logoImage = ImageIO.read(new File("D:\\WorkSpace\\wu\\spring-boot-study\\springboot-demo\\src\\main\\resources\\qiwei\\logo.png"));
            // 计算缩放后的 logo 尺寸
            int maxSize = Math.max(qrCodeImage.getWidth(), qrCodeImage.getHeight()) / 5;
            int logoWidth = logoImage.getWidth(null);
            int logoHeight = logoImage.getHeight(null);
            if (logoWidth > maxSize || logoHeight > maxSize) {
                double scale = Math.min((double)maxSize / logoWidth, (double)maxSize / logoHeight);
                logoWidth = (int)(scale * logoWidth);
                logoHeight = (int)(scale * logoHeight);
            }
            // 在二维码中央放置缩放后的logo
            int qrCodeWidth = qrCodeImage.getWidth(null);
            int qrCodeHeight = qrCodeImage.getHeight(null);
            int x = (qrCodeWidth - logoWidth) / 2;
            int y = (qrCodeHeight - logoHeight) / 2;

            // 缩放logo图片
            Image scaledLogoImage = logoImage.getScaledInstance(logoWidth, logoHeight, Image.SCALE_SMOOTH);
            BufferedImage scaledLogoBufferedImage = new BufferedImage(
                    scaledLogoImage.getWidth(null), scaledLogoImage.getHeight(null), BufferedImage.TYPE_INT_ARGB);
            Graphics graphics = scaledLogoBufferedImage.getGraphics();
            graphics.drawImage(scaledLogoImage, 0, 0, null);
            graphics.dispose();
            // 合并图片
            graphics = qrCodeImage.getGraphics();
            graphics.drawImage(logoImage, x, y, logoWidth, logoHeight, null);
            graphics.dispose();

            // 输出合并后的图片
            ImageIO.write(qrCodeImage, "png", new File("D:\\WorkSpace\\wu\\spring-boot-study\\springboot-demo\\src\\main\\resources\\qiwei\\merged2.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
